//201902104030_何琛选
package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.ActorDao;
import cn.edu.sdjzu.xg.bysj.dao.StudentDao;
import cn.edu.sdjzu.xg.bysj.dao.UserDao;
import cn.edu.sdjzu.xg.bysj.domain.Student;
import cn.edu.sdjzu.xg.bysj.domain.User;
import cn.edu.sdjzu.xg.bysj.domain.authority.Actor;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;
import util.bysjException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class StudentService {
    private StudentService() {}

    private static StudentDao studentDao = StudentDao.getInstance();
    private static StudentService studentService = new StudentService();


    public static StudentService getInstance(){
        return studentService;
    }
    public boolean update(Student student) throws SQLException {
        boolean result = false;
        Connection connection = JdbcHelper.getConn();
        result = studentDao.update(student,connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean add(Student student) throws SQLException,bysjException {
        //获得进行事务的连接，添加student和添加用户都使用本连接对象
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        boolean added = false;
        try{
            //添加actor对象，返回值为自动生成的id
            int actorId = ActorDao.getInstance().add(connection);
            student.setId(actorId);
            //teacherDao添加教师对象，无返回值
            studentDao.add(student, connection);
            //创建用户对象，密码和用户名取自teacher字段值no
            User user = new User(student.getNo(), student.getNo(), null,student);
            //增加用户
            added = UserDao.getInstance().add(user, connection);
            //提交事务
            connection.commit();

        } catch (SQLException e) {
        //回滚事务中所有操作
        e.printStackTrace();
        connection.rollback();
        throw new bysjException("添加失败");
        } finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
		return added;
    }

    public Student find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Student student = studentDao.find(id,connection);
        JdbcHelper.close(connection);
        return student;
    }

    public Collection<Student> findAll
            (List<Condition> conditionList_json_str,
             Pagination pagination)
            throws SQLException {

        Connection connection= JdbcHelper.getConn();
        Collection<Student> students=
                studentDao.findAll(connection,conditionList_json_str,pagination);
        JdbcHelper.close(connection);

        return students;
    }

    public boolean delete(Integer id) throws SQLException, bysjException {
        Connection connection = JdbcHelper.getConn();
        boolean result = false;

        Student student = this.find(id);
        result = this.delete(student);

        JdbcHelper.close(connection);
        return result;
    }

    public boolean delete(Student student) throws SQLException, bysjException {
        boolean result = false;
        Connection connection = JdbcHelper.getConn();

        result = studentDao.delete(student,connection);
        JdbcHelper.close(connection);

        return result;
    }
}